program main
use studentscr
use matrixfuns
use datahadler
use stestmod2
implicit none
integer, parameter:: TT=1000,nn=3,S=10000
integer,parameter:: nvar=2*nn+6
integer iparam(7),T,n,i,j,si,seedvec(S),seedind(2)

double precision:: y(TT,nn),ytot(TT,nn)&
				 &,thscale(nvar),rparam(7),a1,a2,c9

double precision:: thlb(nvar),thub(nvar),thtrue(nvar)&
				 &,th(nvar),llik,grad(nvar),therr(nvar),lika,likb,tha(nvar),thb(nvar)

double precision:: mth(S,nvar),test(S,3),test2(S,3),mateta(S)
!double precision, parameter:: pi=3.14159265358979d0,eta_h1=-.05,p_si=.95d0,b(3)=(/-1.0d-1,-1.0d-1,-1.0d-1/)
!double precision, parameter:: pi=3.14159265358979d0,eta_h1=.2,p_si=.3d0,b(3)=(/-1.0d-1,-1.0d-1,-1.0d-1/)
!double precision, parameter:: pi=3.14159265358979d0,p_si=.999d0,b(3)=(/-1.0d-10,-1.0d-10,-1.0d-10/)

!double precision, parameter:: pi=3.14159265358979d0,eta_h1=.2,p_si=.3d0,b(3)=(/-0.05d0,-0.05d0,-0.05d0/)
double precision, parameter:: pi=3.14159265358979d0,p_si=.999d0,b(3)=(/-1.0d-10,-1.0d-10,-1.0d-10/)
				 
common T,n,y

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
T=TT
n=nn
call rnset(1)
!!!!!!!!!!True parameters
a1=.1d0
a2=.85d0
c9=.999d0

thtrue(1:n)=vassign(n,.2d0)			!delta
thtrue(n+1:2*n-1)=vassign(n-1,1.0d0)	!c
thtrue(2*n)=0.05d0					!alpha0
thtrue(2*n+1)=0.05d0				!phi0
thtrue(2*n+2)=dasin(dsqrt(a1/c9))    !alpha1
thtrue(2*n+3)=dasin(dsqrt(a2/(c9-a1)))   	!alpha2
thtrue(2*n+4)=dasin(dsqrt(a1/c9))		!phi1
thtrue(2*n+5)=dasin(dsqrt(a2/(c9-a1)))	!phi2
thtrue(2*n+6)=.2d0

call readvec(thtrue,2*n+6,"pstheta.txt",1)

!!!!!!!!! Limits

thlb(1:n)=vassign(n,-1.0d2)			   !delta
thlb(n+1:2*n-1)=vassign(n-1,-1.0d2)		   !c
thlb(2*n)=1.0d-10					   !alpha0
thlb(2*n+1)=1.0d-5					   !phi0
thlb(2*n+2:2*n+5)=vassign(4,0.0d0)
thlb(2*n+6)=1.0d-3

thub(1:n)=vassign(n,1.0d2)			   !delta
thub(n+1:2*n-1)=vassign(n-1,1.0d2)		   !c
thub(2*n)=1.0d2 					   !alpha0
thub(2*n+1)=1.0d2					   !phi0
thub(2*n+2:2*n+5)=vassign(4,2.0d0*pi)
thub(2*n+6)=0.2499d0

thscale=vassign(nvar,1.0d0)

call readvecint(seedvec,S,"seedsimst.txt",1)
call readvecint(seedind,2,"seedind.txt",1)


do si=seedind(1),seedind(2),1
print*, si

call rnset(seedvec(si))

!!!!!!!!!!Simulation
!thlb(2*n+6)=thtrue(2*n+6)
!thub(2*n+6)=thtrue(2*n+6)

!call ghsimul2(nvar,T,n,thtrue,eta_h1,b,p_si,ytot)
!call ghsimul1(nvar,T,n,thtrue,eta_h1,b,p_si,ytot)
call stsimul(nvar,T,n,thtrue,ytot)


y=ytot(1:T,1:n)

!!!!!!!!!!Estimation
call du4inf(iparam,rparam)
iparam(3)=1000
iparam(4)=1000
iparam(5)=1000
!rparam(1)=1.0d-7
!rparam(2)=1.0d-15
call dbcong(stllik,stscr,nvar,thtrue,0,thlb,thub&
          &,thscale,1.0d0,iparam,rparam,th,llik)

mateta(si)=th(2*n+6)
mth(si,:)=th
therr=dabs(thub-th)*dabs(th-thlb)
call dsvrgn(nvar,therr,therr)

if (therr(1)>0.0d0) then
call testsample1s(nvar,T,n,th,y,test(si,:))

end if
print*, th(2*n+6)

	call exportvec(vec2(mth(seedind(1):si,:)),"matth2.txt",1)
	call exportrmat(test(seedind(1):si,:),"testst1.txt",1)
    call exportvec(mateta(seedind(1):si),"mateta.txt",1)
end do

end program main